
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
using LPV_POS.Module;
using LPV_POS;
using LPV_POS.mh_pos;
using LPV_POS.mh_pos.mh_ban_hang;
public partial class frmDM_BAN_DAT_BAN : Form
{

    DataTable tbMainDS = new DataTable();
    DataTable tbMain = new DataTable();
    DataTable tbMain1 = new DataTable();//-------06/06/2012----------------
    public string sDM_BAN_ID = "";//FK_    
    public string sDM_BAN_DAT_BAN_ID = "";//FK_
    public static string sDM_BAN_DAT_BAN_ID1 = "";//-----30/05/2012----------
    public string sTHEKHACHHANG = "";
    public string sMA_KH = "";
    Boolean iloadForm = false;
    #region"    frmDM_BAN_DAT_BAN"
    public frmDM_BAN_DAT_BAN()
    {
        InitializeComponent();
    }
    #endregion

    #region"    frmDM_BAN_DAT_BAN_Load"
    public void frmDM_BAN_DAT_BAN_Load(object sender, EventArgs e)
    {
        dtpTHANG_NAM.DateTime = DateTime.Now;
        this.showcombobox();
        this.showdataDS();
        //this.showinfo();
        
     
            iloadForm = true;

    }
    #endregion

    #region"    các hàm load dữ liệu"
    #region"    showdataDS"
    void showdataDS()
    {

        thuvien oclsData = new thuvien();//lớp truy xuat du lieu 
        tbMainDS = oclsData.SP_SelectDataTable("sp_DM_BAN_DAT_BAN",
                           new string[] { "@flag", "@DM_BAN_ID", "@THANG_NAM" },
                           new object[] { 77, sDM_BAN_ID, dtpTHANG_NAM.DateTime });//--------01/06/2012------------------

        grdDM_BAN_DAT_BAN.DataSource = tbMainDS;
        col_KHACH_HANG.BestFit();//----------22/06/2012----Tự động tăng giảm kích thước column theo font chữ
        col_dtVAO.BestFit();
        col_dtRA.BestFit();
    }
    #endregion

    #region"    showdata"
    void showdata()
    {

        thuvien oclsData = new thuvien();//lớp truy xuat du lieu 
        tbMain = oclsData.SP_SelectDataTable("sp_DM_BAN_DAT_BAN",
                           new string[] { "@flag", "@DM_BAN_DAT_BAN_ID", "@DM_BAN_ID" },
                           new object[] { 4, sDM_BAN_DAT_BAN_ID, sDM_BAN_ID });
    }
    #endregion

    #region"    showinfo"
    void showinfo()
    {

        this.showdata();
        //
        btnIn.Enabled = tbMain.Rows.Count > 0;
        btnCheckIn.Enabled = tbMain.Rows.Count > 0;
        btnHuy.Enabled = tbMain.Rows.Count > 0;
        //
        if (tbMain.Rows.Count == 0)
        {

            txtTen_KH.Text = "";
            txtSO_DIEN_THOAI.Text = "";
            dtpNGAY_GIO_DEN.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            num_H1.Value = 0; num_M1.Value = 0;//---02/07/2012-----
            dtpNGAY_GIO_DI.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            num_H2.Value = 0; num_M2.Value = 0;//----02/07/2012----
            numGIA.Value = 0;
            if (cboDM_TIENTE_ID1.Enabled)
            {
                cboDM_TIENTE_ID1.SelectedIndex = 0;
                if (cboDM_TIENTE_ID1.Items.Count > 1)
                    cboDM_TIENTE_ID1.SelectedIndex = 1;
            }
            numDAT_COC.Value = 0;
            if (cboDM_TIENTE_ID2.Enabled)
            {
                cboDM_TIENTE_ID2.SelectedIndex = 0;
                if (cboDM_TIENTE_ID2.Items.Count > 1)
                    cboDM_TIENTE_ID2.SelectedIndex = 1;
            }
            cboGio.Text = DateTime.Now.Hour + "";
            cboPhut.Text = DateTime.Now.Minute + "";
            txtGHI_CHU.Text = "";
            txtTen_KH.Tag = "";

            DataRow drTMP = tbMain.NewRow();
            tbMain.Rows.Add(drTMP);
        }
        else
        {
            txtTen_KH.Text = tbMain.Rows[0]["Ten_KH"] + "";
            txtSO_DIEN_THOAI.Text = tbMain.Rows[0]["SO_DIEN_THOAI"] + "";

            dtpNGAY_GIO_DEN.DateTime = (DateTime)tbMain.Rows[0]["NGAY_GIO_DEN"];
            num_H1.Value = dtpNGAY_GIO_DEN.DateTime.Hour; num_M1.Value = dtpNGAY_GIO_DEN.DateTime.Minute;//---02/07/2012------
            //cboGio.Text = dtpNGAY_GIO_DEN.DateTime.Hour + "";
            cboGio.Text = num_H1.Value.ToString();
            //cboPhut.Text = dtpNGAY_GIO_DEN.DateTime.Minute + "";
            cboPhut.Text = num_M1.Value.ToString();

            if (tbMain.Rows[0]["NGAY_GIO_DI"] + "" != "")
            {
                dtpNGAY_GIO_DI.DateTime = (DateTime)tbMain.Rows[0]["NGAY_GIO_DI"];
                num_H2.Value = dtpNGAY_GIO_DI.DateTime.Hour;
                num_M2.Value = dtpNGAY_GIO_DI.DateTime.Minute;
            }

            numGIA.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["GIA"]).Replace("0-", "-"));
            cboDM_TIENTE_ID1.SelectedValue = tbMain.Rows[0]["DM_TIENTE_ID1"];
            numDAT_COC.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["DAT_COC"]).Replace("0-", "-"));
            cboDM_TIENTE_ID2.SelectedValue = tbMain.Rows[0]["DM_TIENTE_ID2"];
            txtGHI_CHU.Text = tbMain.Rows[0]["GHI_CHU"] + "";
            cboDM_NHOMKHACHHANG.SelectedValue = tbMain.Rows[0]["MANHOMKHACHHANG"];
            txtTen_KH.Tag = tbMain.Rows[0]["CUSTOMER_ID"];
        }

        DataTable tbDM_BAN_DAT_BAN_CT = new thuvien().SP_SelectDataTable("sp_DM_BAN_DAT_BAN_CT",
                                                                    new string[] { "@flag", "@DM_BAN_DAT_BAN_ID" },
                                                                    new object[] { 4, sDM_BAN_DAT_BAN_ID });
        grdDM_BAN_DAT_BAN_CT.DataSource = tbDM_BAN_DAT_BAN_CT;

    }
    #endregion

    #region"    showcombobox"
    void showcombobox()
    {

        thuvien oclsData = new thuvien();

        DataTable tbban = oclsData.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new string[] { "SELECT OBJNAME FROM DM_BAN WHERE OBJID LIKE '" + sDM_BAN_ID + "'" });
        if (tbban.Rows.Count > 0)
            lbel_tencongty.Text = lbel_tencongty.Text + " (" + tbban.Rows[0]["OBJNAME"] + ")";

        DataTable tbDM_TIENTE = oclsData.SP_SelectDataTable("sp_DM_BAN_DAT_BAN",
                                                            new string[] { "@flag" },
                                                            new object[] { 6 });

        cboDM_TIENTE_ID1.DataSource = tbDM_TIENTE.Copy();
        cboDM_TIENTE_ID1.SelectedIndex = 0;

        cboDM_TIENTE_ID2.DataSource = tbDM_TIENTE.Copy();
        cboDM_TIENTE_ID2.SelectedIndex = 0;

        //
        DataTable tbDM_NHOMKHACHHANG = oclsData.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { "SELECT * FROM DM_NHOMKHACHHANG" });
        cboDM_NHOMKHACHHANG.DataSource = tbDM_NHOMKHACHHANG;
        cboDM_NHOMKHACHHANG.DisplayMember = "OBJNAME";
        cboDM_NHOMKHACHHANG.ValueMember = "OBJID";


    }
    #endregion
    #endregion

    #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"

    #region"    btnDSthoat_Click"
    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        //if (MessageBox_LPV.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.OK)
        {
            this.Dispose();
        }

    }
    #endregion

    #endregion

    #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "

    #region"    btnluu_Click"
    private void btnluu_Click(object sender, EventArgs e)
    {
        DateTime den = dtpNGAY_GIO_DEN.DateTime;
        DateTime di = dtpNGAY_GIO_DI.DateTime;
        den =Convert.ToDateTime( den.Year + "/" + den.Month + "/" + den.Day + " " + num_H1.Value+":" + num_M1.Value);
        di = Convert.ToDateTime(di.Year + "/" + di.Month + "/" + di.Day + " " + num_H2.Value + ":" + num_M2.Value);
        //-------------29/05/2012----------------------------------
        string sSQL = "SELECT 1 FROM DM_BAN a INNER JOIN DM_BAN_DAT_BAN dbdb ON a.OBJID=dbdb.DM_BAN_ID "
                       + "  WHERE  ('" + di
                       + "' BETWEEN dbdb.NGAY_GIO_DEN AND dbdb.NGAY_GIO_DI OR '" + den
                       + "' BETWEEN dbdb.NGAY_GIO_DEN AND dbdb.NGAY_GIO_DI OR      dbdb.NGAY_GIO_DEN "
                       + "  BETWEEN '" + den + "' AND '" + di + "' OR dbdb.NGAY_GIO_DI " 
                       + "  BETWEEN '" + den + "' AND '" + di + "' )"
                       + "  AND a.OBJID = '" + sDM_BAN_ID
                       + "' AND dbdb.DM_BAN_DAT_BAN_ID <> '" + sDM_BAN_DAT_BAN_ID + "' AND dbdb.ISDELETE='False'";//DANG CO KHACH
            //-------------29/05/2012----------------------------------
            DataTable tbTMP = new thuvien().SP_SelectDataTable("SP_EXECUTESQL",
                                new string[] { "@flag" },
                                new object[] { sSQL });
            if (tbTMP.Rows.Count > 0)
            {
                MessageBox_LPV.Show("Vui lòng chọn giờ khác");
            }
            else
            {
                if (this.save() > 0)
                {
                    //this.DialogResult = DialogResult.OK;
                    MessageBox_LPV.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //this.Close();
                    this.showdataDS();
                }
            }
        
    
    }

    #endregion
    #region"    save"
    int save()
    {
        DateTime den = dtpNGAY_GIO_DEN.DateTime;
        DateTime di = dtpNGAY_GIO_DI.DateTime;
      den = Convert.ToDateTime(den.Year + "/" + den.Month + "/" + den.Day + " " + num_H1.Value + ":" + num_M1.Value);
      di = Convert.ToDateTime(di.Year + "/" + di.Month + "/" + di.Day + " " + num_H2.Value + ":" + num_M2.Value);
   
        int _iReasult = -1;
        if (this.ValidateSave())
        {
            thuvien oclsData = new thuvien();//lớp truy xuat du lieu
            _iReasult = oclsData.SP_Update("sp_DM_BAN_DAT_BAN",
                                             new string[] { "@flag", "@DM_BAN_ID", "@Ten_KH", 
                                                            "@SO_DIEN_THOAI", "@NGAY_GIO_DEN", 
                                                            "@NGAY_GIO_DI", "@GIA", 
                                                            "@DM_TIENTE_ID1", "@DAT_COC", 
                                                            "@DM_TIENTE_ID2", "@GHI_CHU" ,
                                                            "@CUSTOMER_ID","@MANHOMKHACHHANG", "@DM_BAN_DAT_BAN_ID"
                                             },
                                             new object[] { 1, sDM_BAN_ID, txtTen_KH.Text, 
                                                            txtSO_DIEN_THOAI.Text, den,
                                                            di, numGIA.Value, 
                                                            cboDM_TIENTE_ID1.SelectedValue, numDAT_COC.Value, 
                                                            cboDM_TIENTE_ID2.SelectedValue, txtGHI_CHU.Text,
                                                            txtTen_KH.Tag, cboDM_NHOMKHACHHANG.SelectedValue,sDM_BAN_DAT_BAN_ID
                                             });
        }
        return _iReasult;
    }

    #endregion
    #region"    ValidateSave"
    Boolean ValidateSave()
    {
        if (txtTen_KH.Text.Trim() == "")
        {
            MessageBox_LPV.Show("Nhập tên khách hàng");
            return false;
        }

        return true;
    }

    #endregion

    private void bton_tenhanghoa_Click(object sender, EventArgs e)
    {
        //txtTen_KH
        //mbox_chu mbox_chu;
        mh_chu omh_chu = new mh_chu();
        string s = omh_chu.ShowForm(txtTen_KH.Text, 200, 200, false);
        if (omh_chu.DialogResult == DialogResult.OK)
        {
            txtTen_KH.Text = s;
        }
    }

    #endregion

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        //mh_so omh_so = new mh_so();
        //int i = omh_so.ShowForm();
        //if (omh_so.DialogResult == DialogResult.OK)
        //    txtSO_DIEN_THOAI.Text = i + "";
        string str = new cpublic().loadformso_point(670, 270);
        if (str != "")
            txtSO_DIEN_THOAI.Text = str;
    }

    private void simpleButton2_Click(object sender, EventArgs e)
    {        //
        mh_so omh_so = new mh_so();
        int i = omh_so.ShowForm();
        if (omh_so.DialogResult == DialogResult.OK)
            numGIA.Value = decimal.Parse(i + "");
    }

    private void simpleButton3_Click(object sender, EventArgs e)
    {        //
        mh_so omh_so = new mh_so();
        int i = omh_so.ShowForm();
        if (omh_so.DialogResult == DialogResult.OK)
            numDAT_COC.Value = decimal.Parse(i + "");
    }

    private void simpleButton4_Click(object sender, EventArgs e)
    {        //
        mh_chu omh_chu = new mh_chu();
        string s = omh_chu.ShowForm(txtGHI_CHU.Text, 200, 200, false);
        if (omh_chu.DialogResult == DialogResult.OK)
        {
            txtGHI_CHU.Text = s;
        }
    }

    private void com_nhomkhachhang_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (iloadForm)
        {
            DateTime dt = dtpNGAY_GIO_DEN.DateTime;
            //dtpNGAY_GIO_DEN.DateTime = new DateTime(dt.Year, dt.Month, dt.Day, int.Parse(cboGio.Text), int.Parse(cboPhut.Text), 0);
        }
    }

    private void btnThem_Click(object sender, EventArgs e)
    {
        sDM_BAN_DAT_BAN_ID = "-1";
        this.showinfo();
    }

    private void dtpTHANG_NAM_EditValueChanged(object sender, EventArgs e)
    {
        if (iloadForm)
            this.showdataDS();
    }

    private void lblTen_KH_Click(object sender, EventArgs e)
    {
        if (tbMain.Rows.Count == 0) return;
        if (tbMain.Rows[0]["DM_BAN_DAT_BAN_ID"] + "" != "") return;

        mh_dmkhachhang omh_dmkhachhang = new mh_dmkhachhang();
        DataRow dr = omh_dmkhachhang.ShowForm_fromBanHang("");
        if (omh_dmkhachhang.DialogResult == DialogResult.OK)
        {
            //
            cboDM_NHOMKHACHHANG.SelectedValue = dr["MANHOMKHACHHANG"];
            txtTen_KH.Text = dr["OBJNAME"] + "";
            txtSO_DIEN_THOAI.Text = dr["DIENTHOAIDD"] + "";
            txtTen_KH.Tag = dr["OBJID"];
        }
    }

    private void grvDM_BAN_DAT_BAN_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
    {
        if (grdDM_BAN_DAT_BAN.DataSource != null)
        {
            if (grvDM_BAN_DAT_BAN.RowCount > 0)
            {
                DataRow dr = grvDM_BAN_DAT_BAN.GetDataRow(grvDM_BAN_DAT_BAN.FocusedRowHandle);
                if (dr != null)
                {
                    sDM_BAN_DAT_BAN_ID = dr["DM_BAN_DAT_BAN_ID"] + "";
                    sTHEKHACHHANG = dr["THEKHACHHANG"] + "";
                    sMA_KH = dr["MA_KH"] + "";
                    this.showinfo();
                }
            }
        }
    }

    private void showinfo1()
    {
        throw new NotImplementedException();
    }

    private void btnHuy_Click(object sender, EventArgs e)
    {
       
        if (tbMain.Rows.Count == 0)
        {
            MessageBox_LPV.Show("Chọn thông tin cần hủy");
            return;
        }
        if (tbMain.Rows[0]["DM_BAN_DAT_BAN_ID"] + "" == "")
        {
            MessageBox_LPV.Show("Chọn thông tin cần hủy.");
            return;
        }
        if (sDM_BAN_DAT_BAN_ID == "")
        {
            MessageBox_LPV.Show("Chọn thông tin cần hủy.");
            return;
        }
        DialogResult dlg = MessageBox_LPV.Show("Bạn muốn hủy đặt bàn?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
        if (dlg == DialogResult.OK)
        {
          
            new thuvien().SP_Update("sp_DM_BAN_DAT_BAN", new string[] { "@flag", "@DM_BAN_DAT_BAN_ID" }, new object[] { 8, sDM_BAN_DAT_BAN_ID });

            this.showdataDS();
            this.showinfo();//-------28/06/2012----------
            if (((DataTable)grdDM_BAN_DAT_BAN.DataSource).Rows.Count == 0)//-------28/06/2012----------
                sDM_BAN_DAT_BAN_ID = "-1";
        }
       
    }

    private void dtpNGAY_GIO_DEN_EditValueChanged(object sender, EventArgs e)
    {
        if (iloadForm)
        {
            //if (dtpNGAY_GIO_DEN.DateTime > dtpNGAY_GIO_DI.DateTime)
            //    dtpNGAY_GIO_DI.DateTime = dtpNGAY_GIO_DEN.DateTime;
        }
    }

    private void btnCheckIn_Click(object sender, EventArgs e)
    {
        sDM_BAN_DAT_BAN_ID1 = sDM_BAN_DAT_BAN_ID;//---------30/05/2012------------------------------
        DataTable tb =(DataTable) grdDM_BAN_DAT_BAN.DataSource;
        if (tb.Rows.Count > 0)
        {    
            if (sDM_BAN_DAT_BAN_ID != "-1")
            {
                string sSQL = "SELECT 1 FROM DM_BAN a INNER JOIN BH_PHIEU_TMP b ON b.DM_BAN_MA = a.OBJID AND a.OBJID = '" + sDM_BAN_ID + "'";// Đang có khách
                DataTable tbTMP = new thuvien().SP_SelectDataTable("SP_EXECUTESQL",
                                    new string[] { "@flag" },
                                    new object[] { sSQL });
                if (tbTMP.Rows.Count > 0)
                {
                    MessageBox_LPV.Show("Bàn/phòng này đang có khách.");
                    return;
                }
                else
                    this.DialogResult = DialogResult.OK;
            }
        }
        else
            MessageBox_LPV.Show("Nhập đầy đủ thông tin");
    }


    public int ShowForm()
    {
        this.ShowDialog();

        if (this.DialogResult == DialogResult.OK)
            return int.Parse(sDM_BAN_DAT_BAN_ID);
        return -1;
    }

    private void dtpNGAY_GIO_DI_EditValueChanged(object sender, EventArgs e)
    {
        if (iloadForm)
        {
            //if (dtpNGAY_GIO_DEN.DateTime > dtpNGAY_GIO_DI.DateTime)
            //    dtpNGAY_GIO_DEN.DateTime = dtpNGAY_GIO_DI.DateTime;
        }
    }

    private void btnThem_ChiTiet_Click(object sender, EventArgs e)
    {
        if (sDM_BAN_DAT_BAN_ID != "-1" && sDM_BAN_DAT_BAN_ID != "")
        {
            frmDM_BAN_DAT_BAN_ChonMon frm = new frmDM_BAN_DAT_BAN_ChonMon();
            DataTable tb = frm.ShowForm();
            if (frm.DialogResult == DialogResult.OK)
            {
                DataTable tbDM_BAN_DAT_BAN_CT = (DataTable)grdDM_BAN_DAT_BAN_CT.DataSource;
                foreach (DataRow dr1 in tb.Rows)
                {
                    if (tbDM_BAN_DAT_BAN_CT.Select("MAHH = '" + dr1["OBJID"] + "'").Length == 0)
                    {
                        DataRow dr = tbDM_BAN_DAT_BAN_CT.NewRow();
                        dr["MAHH"] = dr1["OBJID"];
                        dr["TEN_HH"] = dr1["OBJNAME"];
                        dr["SO_LUONG"] = 1;
                        tbDM_BAN_DAT_BAN_CT.Rows.Add(dr);
                    }
                }
                grdDM_BAN_DAT_BAN_CT.DataSource = tbDM_BAN_DAT_BAN_CT;
                grdDM_BAN_DAT_BAN_CT.Refresh();
            }
        }
        else
            MessageBox_LPV.Show("Chọn thông tin đặt bàn cần thêm món");
    }

    private void btnLuuMon_Click(object sender, EventArgs e)
    {
        thuvien othuvien = new thuvien();
        DataTable tbDM_BAN_DAT_BAN_CT = (DataTable)grdDM_BAN_DAT_BAN_CT.DataSource;
        foreach (DataRow dr in tbDM_BAN_DAT_BAN_CT.Rows)
        {
            othuvien.SP_Update("sp_DM_BAN_DAT_BAN_CT",
                            new string[] { "@flag", "@MAHH", "@DM_BAN_DAT_BAN_ID", "@TEN_HH", "@SO_LUONG" },
                            new object[] { 1, dr["MAHH"], sDM_BAN_DAT_BAN_ID, dr["TEN_HH"], dr["SO_LUONG"] });
        }
        if (tbDM_BAN_DAT_BAN_CT.Rows.Count > 0)
        {
            MessageBox_LPV.Show("Lưu thành công");
            if (tbDM_BAN_DAT_BAN_CT.Select("SO_LUONG = 0").Length > 0)
            {
                tbDM_BAN_DAT_BAN_CT = new thuvien().SP_SelectDataTable("sp_DM_BAN_DAT_BAN_CT",
                                                                    new string[] { "@flag", "@DM_BAN_DAT_BAN_ID" },
                                                                    new object[] { 4, sDM_BAN_DAT_BAN_ID });
                grdDM_BAN_DAT_BAN_CT.DataSource = tbDM_BAN_DAT_BAN_CT;
            }
        }
    }

    private void numDAT_COC_ValueChanged(object sender, EventArgs e)
    {

    }

    private void btnIn_Click(object sender, EventArgs e)
    {
        DataTable tbPhieu_CT = (DataTable)grdDM_BAN_DAT_BAN_CT.DataSource;
        LPV_POS.mh_pos.mh_inbaocao.mh_Phieu_dat_ban frm = new LPV_POS.mh_pos.mh_inbaocao.mh_Phieu_dat_ban();
        frm.InPhieu(tbMain, tbPhieu_CT);
    }

    private void numericUpDown2_ValueChanged(object sender, EventArgs e)
    {

    }

}